ShortMaturities = new Object();

ShortMaturities.getCenterPanel = function() {
	
	Ext.create('Ext.data.Store', {
		fields : [
		          { name : 'id',		type : 'int',		},
		          { name : 'dealCode',	type : 'string',	},
		          { name : 'dealType',	type : 'string',	}
		         ],
		storeId:'shortMaturityDealStore',
		});

	var centerPanel =  Ext.create('Ext.form.Panel', {
		title : 'Short Maturities',
		bodyPadding : '20',
		autoScroll : true,
		defaults : {
			labelWidth : 200,
			anchor : '65%'
		},
		frame : true,
		items : [ {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'combo',
				id : 'shortMaturityDealTypeCombo',
				fieldLabel : 'Deal Type',
				displayField : 'name',
				disabled : true,
				flex : 1,
				padding : '0 15 0 0',
				valueField : 'value',
				queryMode : 'local',
				store : Ext.create('Ext.data.Store', {
					fields : [ 'name', 'value' ],
					data : [
					        	{	name : 'PURCHASE',		value : 'PURCH'	},
					        	{	name : 'FD',			value : 'FD'	},
					        	{	name : 'REPO',			value : 'REPO'	},
					        	{	name : 'REVERSE REPO',	value : 'RREPO'	}
					       ]
				}),
				listeners : {
					select : {
						fn : function(combo, value) {
							Ext.data.StoreManager.lookup('shortMaturityDealStore').clearFilter(true);
							Ext.data.StoreManager.lookup('shortMaturityDealStore').filter(
									[
									 Ext.create('Ext.util.Filter', {
										 filterFn: function(item) {
											 return (item.get("dealType")===combo.getValue());
										 },
										 root: 'data' })
									]);
							Ext.ComponentQuery.query('#shortMaturityDealCombo')[0].clearValue();
							Ext.ComponentQuery.query('#shortMaturityMatDate')[0].reset();
							Ext.ComponentQuery.query('#shortMaturityValDate')[0].reset();
							Ext.ComponentQuery.query('#shortMaturityActRate')[0].reset();
							Ext.ComponentQuery.query('#shortMaturityDealVal')[0].reset();
							Ext.ComponentQuery.query('#shortMaturityNewMatDate')[0].reset();
							Ext.ComponentQuery.query('#shortMaturityNewActRate')[0].reset();
							Ext.ComponentQuery.query('#shortMaturityNewMatVal')[0].reset();
							Ext.ComponentQuery.query('#shortMaturityMatVal')[0].reset();
							Ext.ComponentQuery.query('#shortMaturityNewMatDate')[0].disable();
						}
					}
				},
			}, {
				xtype : 'combo',
				id : 'shortMaturityDealCombo',
				labelWidth : 120,
				fieldLabel : 'Deal',
				valueField : 'id',
				displayField : 'dealCode',
				name : 'dealId',
				flex : 1,
				store : Ext.data.StoreManager.lookup('shortMaturityDealStore'),
				queryMode : 'local',
				autoScroll : true,
				allowBlank : false,
				disabled : true,
				listeners : {
					select : {
						fn : function(combo, value) {
							var tempDealStore = Ext.data.StoreManager.lookup('shortMaturityDealStore');
							var dealType =(tempDealStore.getAt(tempDealStore.find('id', combo.getValue()))).get('dealType');
							Ext.ComponentQuery.query('#shortMaturityDealTypeCombo')[0].setValue(dealType);
							
							Ext.Ajax.request({
								url : 'queryDeal.action',
								params : {
									id : combo.getValue(),
								},
								success : function(response) {
									var obj = Ext.decode(response.responseText);
									
									Ext.ComponentQuery.query('#shortMaturityMatDate')[0].reset();
									Ext.ComponentQuery.query('#shortMaturityValDate')[0].reset();
									Ext.ComponentQuery.query('#shortMaturityActRate')[0].reset();
									Ext.ComponentQuery.query('#shortMaturityDealVal')[0].reset();
									Ext.ComponentQuery.query('#shortMaturityNewMatDate')[0].reset();
									Ext.ComponentQuery.query('#shortMaturityNewActRate')[0].reset();
									Ext.ComponentQuery.query('#shortMaturityNewMatVal')[0].reset();
									Ext.ComponentQuery.query('#shortMaturityMatVal')[0].reset();									
									
									var allowed = true;
									var notAllowedMsg = '';
									
									if(obj.deal.dealType.code==='PURCH'){
										if(obj.deal.dealInstrumentMaps.length==1){
											if(obj.deal.dealInstrumentMaps[0].instrument.instrumentType.shortMaturable===0){
												allowed = false;
												notAllowedMsg = 'Short Maturity Not allowed for '+obj.deal.dealInstrumentMaps[0].instrument.instrumentType.instrumentName;
											}
											
											if(obj.deal.dealInstrumentMaps[0].instrument.status!='IN'){
												allowed = false;
												notAllowedMsg = 'Instrument is being used for another deal: InsCode: '+obj.deal.dealInstrumentMaps[0].instrument.instrumentCode;
											}
										}
									}									
									if(!allowed){
										Ext.Msg.alert('Error', notAllowedMsg);
										Ext.ComponentQuery.query('#shortMaturityNewMatDate')[0].disable();
									}else{
										var matDtLocal = Ext.ComponentQuery.query('#shortMaturityMatDate')[0];
										var tmpDate = Ext.Date.parse(obj.deal.maturityDate, 'c');
										matDtLocal.setValue(tmpDate);
										Ext.ComponentQuery.query('#shortMaturityNewMatDate')[0].setMaxValue(tmpDate);
										var valDtLocal = Ext.ComponentQuery.query('#shortMaturityValDate')[0];
										tmpDate = Ext.Date.parse(obj.deal.valueDate, 'c');
										valDtLocal.setValue(tmpDate);
											
										var actRateLocal = Ext.ComponentQuery.query('#shortMaturityActRate')[0];
										actRateLocal.setValue(obj.deal.actualRate);
										
										var dealValLocal = Ext.ComponentQuery.query('#shortMaturityDealVal')[0];
										dealValLocal.setValue(Ext.util.Format.number(obj.deal.dealValue, '0,000.0000'));
										//this.setRawValue(Ext.util.Format.number(obj.deal.dealValue, '0,000.0000'));
										
										var matValLocal = Ext.ComponentQuery.query('#shortMaturityMatVal')[0];
										matValLocal.setValue(Ext.util.Format.number(obj.deal.maturityValue, '0,000.0000'));
										Ext.ComponentQuery.query('#shortMaturityNewMatDate')[0].enable();
									}
									console.log(obj.deal.dealCode);
									console.log(obj.deal.actualRate);
									console.log(obj.deal.maturityDate);
									console.log(obj.deal.valueDate);
									console.log(obj.deal.dealValue);
									
								},
								failure : function(){
									Ext.Msg.alert('Error', 'Failed to Load Deal Deatils!');
								}
							});
						}
					}
				},
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ 
				{
				    xtype : 'datefield',
				    fieldLabel : 'Value Date',
				    id : 'shortMaturityValDate',
				    flex : 1,
				    readOnly : true,
				    disabled : true,
				    format : 'd/m/Y',
				    padding : '0 15 0 0'
				},
				{
					xtype : 'textfield',
					labelWidth : 120,
					id : 'shortMaturityDealVal',
					fieldLabel : 'Deal Value',
					readOnly : true,
					disabled : true,
					flex : 1,
					fieldStyle : "text-align:right;",
					maskRe : /[\d\,.]/,
					listeners : {
						blur : function() {
							this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
							console.log(Ext.util.Format.number(this.value, '0,000.00'));
						},
						render : function() {
							this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
						}
					}
				} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'datefield',
				id : 'shortMaturityMatDate',
				fieldLabel : 'Maturity Date',
				flex : 1,
				readOnly : true,
				disabled : true,
				format : 'd/m/Y',
				padding : '0 15 0 0'
			}, {
				xtype : 'datefield',
				id : 'shortMaturityNewMatDate',
				labelWidth : 120,
				disabled:true,
				format : 'd/m/Y',
				fieldLabel : 'New Maturity Date',
				flex : 1,
				minValue : new Date(),
				allowBlank:false,
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				id : 'shortMaturityActRate',
				fieldLabel : 'Actual Rate',
				flex : 1,
				readOnly : true,
				disabled : true,
				padding : '0 15 0 0'
			}, {
				xtype : 'numberfield',
				labelWidth : 120,
				id : 'shortMaturityNewActRate',
				name : 'newActualRate',
				fieldLabel : 'New Actual Rate',
				flex : 1,
				minValue : 0,
				maxValue : 100,
				decimalPrecision:4,
				allowBlank:false,
			} ]
		}, {
			xtype : 'fieldcontainer',
			layout : 'hbox',
			items : [ {
				xtype : 'textfield',
				fieldLabel : 'Maturity value',
				flex : 1,
				id : 'shortMaturityMatVal',
				readOnly : true,
				padding : '0 15 0 0',
				disabled : true,
				fieldStyle : "text-align:right;",
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
						console.log(Ext.util.Format.number(this.value, '0,000.00'));
					},
					render : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			}, {
				xtype : 'textfield',
				labelWidth : 120,
				fieldLabel : 'New Maturity value',
				id : 'shortMaturityNewMatVal',
				flex : 1,
				disabled : true,
				fieldStyle : "text-align:right;",
				maskRe : /[\d\,.]/,
				listeners : {
					blur : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
						console.log(Ext.util.Format.number(this.value, '0,000.00'));
					},
					render : function() {
						this.setRawValue(Ext.util.Format.number(this.value, '0,000.0000'));
					}
				}
			} ]
		} ],
		buttonAlign : 'center',
		buttons : [ {
			text : 'Cancel',
			handler : function() {
				App.Core.removeScreen();
			}
		}, {
			text : 'Reset',
			handler : function() {
				this.up('form').getForm().reset();
			}
		}, {
			text : 'Save',
			id : 'shortMaturitySaveBtn',
			itemId : 'saveButton',
			handler : function() {
				var form = this.up('form').getForm();
			
				form.url = 'newShortMat.action';
				if (form.isValid()) {
					centerPanel.setLoading('Saving...');
					
					var rstDt = Ext.ComponentQuery.query('#recPurchFloatResetDt')[0];
					var valueDtField = Ext.ComponentQuery.query('#recPurchValueDt')[0];
					var newMaturityDtField = Ext.ComponentQuery.query('#shortMaturityNewMatDate')[0];
					console.log(Ext.util.Format.date(newMaturityDtField.getValue()));
					form.submit({						
						params : {
							'newMaturityDate'	: Ext.util.Format.date(newMaturityDtField.getValue()),
							'maturityValue'	: 2500.0000,
							'user'			: 'shrtMatUser',
						},
						success : function(form, action) {
							form.reset();
							Ext.Msg.alert('Success', 'Short Maturity Completed.<br> Temperory Deal Code is <b>'+action.result.dealCode+'</b>');
							centerPanel.setLoading(false);
						},
						failure : function(form, action) {
							Ext.Msg.alert('Failed', 'There is some error returned from the server.<br><b>'+action.result.status+'</b>');
							centerPanel.setLoading(false);
						}
					});
				}else{
					Ext.Msg.alert("Form Incomplete. Please check!");
				}
			}
		} ]

	});
	
	centerPanel.setLoading(true);
	Ext.Ajax.request({
		url : 'getConfirmedDeals.action',
		success : function(response) {
			var objDeal = Ext.decode(response.responseText);
			//Ext.data.StoreManager.lookup('shortMaturityDealStore').loadRawData(objDeal.dealList);			
			if(objDeal.dealList!=null){
				var arrayLength = objDeal.dealList.length;
				for (var i = 0; i < arrayLength; i++) {
					Ext.data.StoreManager.lookup('shortMaturityDealStore').add(
				    		{
				    			id			:	objDeal.dealList[i].id,
				    			dealCode	:	objDeal.dealList[i].dealCode,
				    			dealType	:	objDeal.dealList[i].dealType.code,
				    		});
				}									
			}
			Ext.ComponentQuery.query('#shortMaturityDealCombo')[0].enable();
			Ext.ComponentQuery.query('#shortMaturityDealTypeCombo')[0].enable();
			centerPanel.setLoading(false);
		},
		failure : function(response){
			centerPanel.setLoading(false);
			Ext.Msg.alert('Error!', 'Failed to load Deal List : '+response);
		},
	});
	
	return centerPanel;
};